<template>
  <div class="app-container">
    <avue-crud
      :option="option"
      :data="data"
      :table-loading="loading"
      :page.sync="page"
      @on-load="onLoad"
      @row-save="rowSave"
      @row-update="rowUpdate"
      @row-del="rowDel"
      :before-open="beforeOpen"
    >
      <template
        slot="icon"
        slot-scope="scope"
      >
        <div>
          <svg-icon :icon-class="scope.row.icon || '404'"></svg-icon>
        </div>
      </template>
    </avue-crud>
  </div>
</template>

<script>
import { addCUser, upCUser, delCUser, getCUser } from '@/api/manager.js'
export default {
  data() {
    return {
      data: [],
      /* 表格系列 */
      loading: false,
      /* 分页 */
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
        code: '',
        background: false,
      },
      option: {
        headerAlign: 'center',
        align: 'center',
        border: true,
        index: true,
        indexLabel: '序号',
        indexWidth: 100,
        card: true,
        defaultExpandAll: true,
        dialogDrag: false,
        menuWidth: 200,
        column: [
          {
            label: '登录名',
            prop: 'loginName',
          },
          {
            label: '中文名',
            prop: 'userName',
          },
          {
            label: '工号',
            prop: 'eid',
          },
          {
            label: '用户别名',
            prop: 'userNickName',
          },
          {
            label: '职位',
            prop: 'post',
          },
          {
            label: '性别',
            prop: 'userGender',
            type: 'select',
            dicData: [
              {
                label: '男',
                value: true,
              },
              {
                label: '女',
                value: false,
              },
            ],
          },
          {
            label: '地址',
            prop: 'userAddress',
            hide: true,
          },
          {
            label: '密码',
            prop: 'loginPass',
            type: 'password',
            hide: true,
          },
          {
            label: '密码确认',
            prop: 'passwdConfirm',
            type: 'password',
            width: 150,
            hide: true,
          },
          {
            label: '部门编码',
            prop: 'deptCode',
            hide: true,
          },

          {
            label: '电话',
            prop: 'userPhone',
          },
          {
            label: '邮箱',
            prop: 'userEmail',
            width: 180,
          },
          {
            label: '邮政编码',
            prop: 'userZip',
            hide: true,
          },
          {
            label: '是否有效',
            prop: 'accountIsvalid',
            type: 'select',
            wdith: 100,
            value: true,
            dicData: [
              {
                label: '有效',
                value: true,
              },
              {
                label: '无效',
                value: false,
              },
            ],
          },
          {
            label: '是否启用',
            prop: 'accountLocked',
            type: 'select',
            wdith: 100,
            value: true,
            dicData: [
              {
                label: '是',
                value: true,
              },
              {
                label: '否',
                value: false,
              },
            ],
          },
          {
            label: '备注',
            prop: 'userDesc',
            type: 'textarea',
            span: 24,
            width: 200,
            overHidden: true,
          },
        ],
      },
    }
  },
  methods: {
    onLoad(page, params = {}) {
      this.loading = true
      console.log(page)
      params = {
        page: page.currentPage,
        size: page.pageSize,
      }
      getCUser(params).then((data) => {
        if (data.status) {
          this.loading = false
          this.data = data.data.records
          this.page.total = data.data.total
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    /* 新增 */
    rowSave(row, done, loading) {
      delete row.createTime
      delete row.updateTime
      addCUser(row).then(
        (data) => {
          if (data.status) {
            done()
            this.onLoad(this.page)
            this.$message({
              type: 'success',
              message: '操作成功!',
            })
          } else {
            loading()
            this.$message.error('系统错误,请稍候再试!')
          }
        },
        (error) => {
          window.console.log(error)
          loading()
        }
      )
    },
    /* 修改 */
    rowUpdate(row, index, done, loading) {
      delete row.createTime
      delete row.updateTime
      upCUser(row).then(
        (data) => {
          if (data.status) {
            done()
            this.onLoad(this.page)
            this.$message({
              type: 'success',
              message: '操作成功!',
            })
          } else {
            this.$message.error(data.msg)
            loading()
          }
        },
        (error) => {
          window.console.log(error)
          loading()
        }
      )
    },
    /* 删除 */
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        delCUser({
          id: row.id,
        }).then((data) => {
          if (data.status) {
            this.onLoad(this.page)
            this.$message({
              type: 'success',
              message: '操作成功!',
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },
    /* 打开弹窗前回调 */
    beforeOpen(done, type) {
      if (['edit', 'add'].includes(type)) {
        // getResTree().then(data => {
        //     if (data.status) {
        //         let i = this.findArray(this.option.column, 'resPid', 'prop');
        //         if (i > -1) {
        //             this.option.column[i].dicData = data.data.records;
        //         }
        //     } else {
        //         this.$message.error("系统错误,请稍候再试!")
        //     }
        // });
      }
      done()
    },
  },
}
</script>

<style scoped="scoped" lang="scss">
/deep/ .avue-crud__pagination {
  text-align: center;
}
</style>
